package com.banciyuan.bcywebview.biz.write.write;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.DailyApi;
import com.banciyuan.bcywebview.api.GroupApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class WriteHelper {

    public static final int PICK_IMAGE = 1;
    public static final int MAX_PIC_NUM = 9;

    private Context mContext;
    private IWriteInter iWriteInter;
    private RequestQueue mQueue;

    private boolean send_flag;

    public WriteHelper(Context mContext, IWriteInter iWriteInter) {
        this.mContext = mContext;
        this.iWriteInter = iWriteInter;
        this.mQueue = VolleyQueue.getRquest(mContext);
    }

    public void doPush(String type, String gid, List<String> mPath, String mContent, String work, String tag_names, String authority) {
        if (send_flag) return;
        send_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    iWriteInter.writeSucc(response);
                } else {
                    iWriteInter.writeFail(response);
                }
                send_flag = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iWriteInter.writeFail("");
                send_flag = false;
            }
        };
        List<BcyPair> paramsList = new ArrayList<>();
        String url = "";
        if (type.equals("post")) {
            url = HttpUtils.BASE_URL + GroupApi.getNewpost();
            if (mPath != null) {
                paramsList.add(new BcyPair(HttpUtils.GROUP_PATHS, new JSONArray(mPath).toString()));
            }
            paramsList.add(new BcyPair(HttpUtils.GID, gid));
        } else if (type.equals("daily")) {
            url = HttpUtils.BASE_URL + DailyApi.getDonewpost();
            if (mPath != null) {
                paramsList.add(new BcyPair(HttpUtils.PATH, new JSONArray(mPath).toString()));
            }
            paramsList.add(new BcyPair(HttpUtils.AUTHORITY, authority));
        }
        if (!TextUtils.isEmpty(work)) {
            paramsList.add(new BcyPair(HttpUtils.GROUP_WORK, work));
        }
        if (!TextUtils.isEmpty(tag_names)) {
            paramsList.add(new BcyPair(HttpUtils.TAG_NAMES, tag_names));
        }
        paramsList.add(new BcyPair(HttpUtils.DAILY_CONTENT, mContent));
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));

        Map<String, String> map = HttpUtils.getData(paramsList);
        StringRequestParam getUserdataRequest = new StringRequestParam(Method.POST, url, map, onSuccess, onError);
        getUserdataRequest.setRetryPolicy(new DefaultRetryPolicy(60000, 0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        mQueue.add(getUserdataRequest);
    }

    public void doEditPush(String ud_id, String mContent, String work, String authority) {
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    iWriteInter.writeSucc(response);
                } else {
                    iWriteInter.writeFail(response);
                }
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iWriteInter.writeFail("");
            }
        };
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = HttpUtils.BASE_URL + DailyApi.doEditDaily();
        paramsList.add(new BcyPair(HttpUtils.UDID, ud_id));
        paramsList.add(new BcyPair(HttpUtils.AUTHORITY, authority));
        if (!TextUtils.isEmpty(work)) {
            paramsList.add(new BcyPair(HttpUtils.GROUP_WORK, work));
        }
        paramsList.add(new BcyPair(HttpUtils.DAILY_CONTENT, mContent));
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));

        Map<String, String> map = HttpUtils.getData(paramsList);
        StringRequestParam getUserdataRequest = new StringRequestParam(Method.POST, url, map, onSuccess, onError);
        getUserdataRequest.setRetryPolicy(new DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        mQueue.add(getUserdataRequest);
    }
}